[리트코드]_20.Valid Parentheses


문제 주소

https://leetcode.com/problems/valid-parentheses/

My Solution

def isValid(s):
    stack = []
    table = {
        ')' : '(',
        '}' : '{',
        ']' : '[',
    }
    # 스택 이용 예외 처리 및 일치 여부 판별
    for char in s:
        if char not in table:
            stack.append(char)
        elif not stack or table[char] != stack.pop():
            return False


    return len(stack) == 0

print(isValid('()[]{}'))

풀이방법

table를 정의해놓고 매핑해 해당 테이블과 매핑이 안되는 스택은 False, 나머진 True을 이용해 풀이


Hello, I'm@nickhealthy
개발자를 꿈꾸고, 파이썬과 클라우드에 관심이 많은 비전공자

Github